import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'
import dts from 'vite-plugin-dts'

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  const isLibrary = command === 'build'
  
  if (isLibrary) {
    // 库构建配置 - 生产环境，导出纯TypeScript
    return {
      publicDir: false, // 不包含public文件夹
      plugins: [
        dts({
          insertTypesEntry: true,
          rollupTypes: true,
        })
      ],
      resolve: {
        alias: {
          '@': resolve(__dirname, 'src'),
        },
      },
      build: {
        lib: {
          entry: resolve(__dirname, 'src/lib.ts'),
          name: 'PixiJSCharacterSDK',
          fileName: (format) => `pixijs-character-ittantt-sdk.${format}.js`,
          formats: ['es', 'umd']
        },
        rollupOptions: {
          external: ['pixi.js', 'react', 'react-dom'],
          output: {
            globals: {
              'pixi.js': 'PIXI',
              'react': 'React',
              'react-dom': 'ReactDOM'
            },
            exports: 'named' // 避免default导出警告
          }
        },
        minify: 'terser',
        terserOptions: {
          compress: {
            drop_console: true,  // 移除console.log
            drop_debugger: true, // 移除debugger
            pure_funcs: ['console.log', 'console.warn', 'console.error']
          }
        },
        sourcemap: false, // 生产环境不需要sourcemap
        target: 'es2018'
      }
    }
  } else {
    // 开发配置 - 开发环境，使用React演示
    return {
      plugins: [react()],
      resolve: {
        alias: {
          '@': resolve(__dirname, 'src'),
        },
      },
      root: '.'
    }
  }
})